package com.example.brazil_project.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import com.example.brazil_project.parameter.Parameter;
import com.example.brazil_project.parameter.SharedPreferencesParameter;
import com.example.brazil_project.service.MsgService;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
import android.webkit.WebView;

public class ThreadUtil implements Runnable {

	private Activity Contxt;
	private Object view;
	private String type;

	private String title;
	private String msg;
	private int msgid;
	private String ioc;
	private String rang;
	private WebView webview ;
	private String value;
	boolean isrun =true;
	/**
	 * 
	 * @param Contxt Activity 上下文
	 * @param view   执行的view
	 * @param type	  执行类型
	 */
	
	public ThreadUtil(String type,String value){
		this.type=type;
		this.value=value;
		
	}
	public ThreadUtil(Activity Contxt , Object view,String type){
		this.Contxt =Contxt;
		this.view=view;
		this.type=type;
		 webview = (WebView) view;
	}
	public ThreadUtil(Activity Contxt ,String title,String msg,String type,int msgid,String ioc,String rang){
		this.Contxt =Contxt;
		this.title = title;
		this.msg = msg;
		this.type = type;
		this.msgid = msgid;
		this.ioc =ioc;
		
	}
	@Override
	public void run() {
			this.Contxt = Parameter.openingContx;
			List list1 = new ArrayList();
			list1.add("username");
			Map <String,String> map1 =  SharedPreferencesUtil.getSharedPreferencesData( this.Contxt, "u_p", list1) ;
			String  username = map1.get("username");
			String First_Time = Parameter.getFirst_Time();
			//没有登录则不跑后台线程
			if(username==null||username.length()<=0){			
				return;
			}
			try {
			while (isrun) {
				
				 if(type.equals("getJsReturn")){
					 
					 webview.loadUrl("javascript:getalertType()");
					 isrun= false;
				 }
				 if(type.equals("Login_BackPressed")){
					 isrun= false;
					 if (Parameter.getJsReturn.equals("1")){
						  
						  webview.loadUrl("javascript:close_select()");
					  }else{
						  Contxt.finish();
					}
				 }
				 if(type.equals("sendN")){
					 isrun= false;
					 SendMsgUtil.sengNotification( Contxt, title, msg, rang,ioc,msgid);
					
				 }
				 
				 if(type.equals("post")){
					 isrun= false;
					 SendMsgUtil.sengNotification( Contxt, title, msg, rang,ioc,msgid);
					
				 }
				 
				 if(type.equals("startService")){
					Log.i("http", value);
					
				 }
				 
				 
				 
				 //获取用户的系统信息	
				 if(type.equals("getSystemInform")){
					synchronized (First_Time) {					
						 List<NameValuePair> params = new ArrayList<NameValuePair>();  
					     params.add(new BasicNameValuePair("create_date",First_Time));  
					     params.add(new BasicNameValuePair("username",username)); 
					     
						 String result = HttpUtil.doPost(value,params);
						 List<Map<String, Object>>list = jsonUtil.getlistForJson(result);
						
						 //存放系统信息
						 String sysmsg =null;
						 String yjfkmsg =null;
						 if(list!=null&&list.size()>0){
							 for (Map map : list) {
								 String selectsql = "SELECT * FROM SystemInform_table WHERE id = "+map.get("id")+" AND username ='"+username+"'";
								 Cursor c1 = SQLiteDatabaseUtil.select(selectsql);
								 //如果这条系统系统信息已经传给这位客户了，就不需要再传给他们
								 if(c1!=null&&c1.getCount()>0){
									 
									 continue;
								 }
								 
								 String insertSql = "INSERT INTO SystemInform_table VALUES ('"+map.get("id")+"','"+map.get("title")+"','"+map.get("content")+"','"+map.get("CreateDate")+"'"
								 		+ ",'"+username+"','"+0+"','"+map.get("msgType")+"','"+map.get("reply")+"')";
								 SQLiteDatabaseUtil.insert(insertSql);
								 
								 //如果有系统消息提示，一次多条也只发一次信息
								 if(sysmsg ==null&&map.get("msgType").toString().equals("1")){
									 SendMsgUtil.sengNotification(this.Contxt, map.get("title").toString(), map.get("content").toString(), rang,"assets/img/msg_xt.png",1);
									 sysmsg = "sent";
								 }
								 if(yjfkmsg ==null&&map.get("msgType").toString().equals("2")){
									 SendMsgUtil.sengNotification( this.Contxt, map.get("title").toString(), map.get("content").toString(), rang,"assets/img/msg_xt.png",1);
									 yjfkmsg = "sent";
								 }	
								 }
		
						 }
								Thread.sleep(120000);
						
					 }
			}
				 
				
				
			}
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	
		

}
